<?php
/**
 * Introduction: 收货地址接口类
 * @author: 王龙
 * @date: 2016/4/18 17:37
 * @email: wanglong@cdlinglu.com
 */

namespace App\Controller\HarvestAddress;

use Common\Controller\App\SignController;

class IndexController extends SignController
{

    /**
     * Introduction: 获取收货地址列表
     * @author: 王龙
     * @date: 2016/4/18 17:50
     * @email: wanglong@cdlinglu.com
     */
    public function lists()
    {

        $HarvestAddress = D('Common/HarvestAddress', 'Logic');


        outPut($HarvestAddress::getData(['uid' => self::$uid]), true);//查询所有收货地址

    }

    /**
     * Introduction: 增加收货地址
     * @author: 王龙
     * @date: 2016/4/18 17:52
     * @email: wanglong@cdlinglu.com
     *
     * //数据格式
     * var data = {
     * data:{
     * 'recipient': '杨陈鹏',//收货人姓名
     * 'sex' : '1',//收货人性别
     * 'mobile' : '13123144888',//电话号码
     * 'province': '3443232', //省份ID
     * 'province_name':'四川省',//省份名称
     * 'city'  :'45343',//市区ID
     * 'city_name'  :'达州市',//市名称
     * 'county'  :'45343',//县级ID
     * 'county_name'  :'大竹县',//县级名称
     * 'address'  :'清水镇',//详细地址
     * 'default'  :1,//是否设为默认
     * }
     * }
     */
    public function create()
    {

        $harvest_address = D('Common/HarvestAddress', 'Logic');

        $data = I('post.data');

        $data['uid'] = self::$uid;

        outPut($harvest_address::create($data));
    }

    /**
     * Introduction: 获取一条收货地址详情
     * @author: 杨陈鹏
     * @date: 2016/4/19 10:07
     * @email: wanglong@cdlinglu.com
     */
    public function detail()
    {

        $id = I('post.id', false);

        $where['id'] = $id;
        $where['uid'] = self::$uid;

        if (!$id)
            unset($where['id']);


        $logic = D('Common/HarvestAddress', 'Logic');

        outPut($logic::getOneData($where));

    }

    /**
     * Introduction: 编辑收货地址
     * @author: 王龙
     * @date: 2016/4/18 17:52
     * @email: wanglong@cdlinglu.com
     *
     * var data = {
     * data:{
     * 'id' : 16,
     * 'recipient': '刘伟光',//收货人姓名
     * 'sex' : '1',//收货人性别
     * 'mobile' : '13123144888',//电话号码
     * 'province': '3443232', //省份ID
     * 'province_name':'四川省',//省份名称
     * 'city'  :'45343',//市区ID
     * 'city_name'  :'达州市',//市名称
     * 'county'  :'45343',//县级ID
     * 'county_name'  :'大竹县',//县级名称
     * 'address'  :'清水镇',//详细地址
     * 'default'  :1,//是否设为默认
     * }
     * }
     */
    public function update()
    {

        $harvest_address = D('Common/HarvestAddress', 'Logic');

        $data = I('post.data');

        $id = I('post.id', false);

        if ($id) {

            $data = [];

            $data['id'] = $id;

            $set_default = true;

        }

        $data['uid'] = self::$uid;

        outPut($harvest_address::update($data, $set_default));
    }

    /**
     * Introduction: 删除收货地址
     * @author: 王龙
     * @date: 2016/4/18 17:52
     * @email: wanglong@cdlinglu.com
     */
    public function delete()
    {

        $harvest_address = D('Common/HarvestAddress', 'Logic');

        $data['ids'] = trim(I('post.ids'), ',');

        $data['uid'] = self::$uid;

        outPut($harvest_address::delete($data), true);
    }
}